<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: ad_permissions.php 4 2008-08-03 01:43:03Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * This is the admin interface for permissions of the Fusion Registry
 *
 * @package Fusion Registry
 * @subpackage admin
 * @version $Id: ad_permissions.php 4 2008-08-03 01:43:03Z registry.fusion $
 */

if ( ! defined( 'AD_REGISTRY_CALLED' ) )
{
    print "<h1>Incorrect access</h1>You cannot access this file directly.";
    exit();
}

/**
 * Fusion Registry requires
 */
require_once(AD_REGISTRY_PATH.'ad_registry_loader.php');

/**
 * Class ad_registry_perms deals with permissions of the Fusion Registry
 *
 * @package Fusion Registry
 * @subpackage admin
 */
class ad_registry_permissions
{
    var $ipsclass;

    /** @var string */
    var $base_url;

    /** @var object */
    var $ad_registry_loader;

    /**
     * Intialization of ad_registry_perms
     *
     * Another traffic cop function, there should be minimal procedural code
     * here.
     *
     * @global array
     */
    function auto_run()
    {
        // Kill globals
        $tmp_in = array_merge( $_GET, $_POST, $_COOKIE );

        foreach ( $tmp_in as $k => $v )
        {
            unset($$k);
        }

        // Initialize ad_registry_loader
        $this->ad_registry_loader = new ad_registry_loader();
        $this->ad_registry_loader->ipsclass =& $this->ipsclass;
        $this->ad_registry_loader->auto_run(&$this);

        switch($this->ipsclass->input['code'])
        {
            case 'config':
                $this->save_config();
                break;

            default:
                $this->main_menu();
                break;
        }
    }


    /**
     * Build the HTML for permission access levels
     *
     * Key: read=browse write=add reply=interact (guestbook comments/ratings)
     *
     * @param string $browse
     * @param string $add
     * @param string $interact
     * @param string $upload
     * @return string HTML source
     */
    function build_registry_perms( $browse='*', $add='*', $interact='*', $upload='*' )
    {
        $html = "
                <script language='Javascript1.1'>
                <!--

                function check_all(str_part)
                {
                    var f = document.theAdminForm;

                    for (var i = 0 ; i < f.elements.length; i++)
                    {
                        var e = f.elements[i];

                        if ( (e.name != 'UPLOAD_ALL') && (e.name != 'BROWSE_ALL') && (e.name != 'INTERACT_ALL') && (e.name != 'ADD_ALL') && (e.type == 'checkbox') && (! e.disabled) )
                        {
                            s = e.name;
                            a = s.substring(0, 4);

                            if (a == str_part)
                            {
                                e.checked = true;
                            }
                        }
                    }
                }

                function obj_checked(IDnumber)
                {
                    var f = document.theAdminForm;

                    str_part = '';

                    if (IDnumber == 1) { str_part = 'BROW' }
                    if (IDnumber == 2) { str_part = 'INTE' }
                    if (IDnumber == 3) { str_part = 'ADD_' }
                    if (IDnumber == 4) { str_part = 'UPLO' }

                    totalboxes = 0;
                    total_on   = 0;

                    for (var i = 0 ; i < f.elements.length; i++)
                    {
                        var e = f.elements[i];

                        if ( (e.name != 'UPLOAD_ALL') && (e.name != 'BROWSE_ALL') && (e.name != 'INTERACT_ALL') && (e.name != 'ADD_ALL') && (e.type == 'checkbox') )
                        {
                            s = e.name;
                            a = s.substring(0, 4);

                            if (a == str_part)
                            {
                                totalboxes++;

                                if (e.checked)
                                {
                                    total_on++;
                                }
                            }
                        }
                    }

                    if (totalboxes == total_on)
                    {
                        if (IDnumber == 1) { f.BROWSE_ALL.checked  = true; }
                        if (IDnumber == 2) { f.INTERACT_ALL.checked = true; }
                        if (IDnumber == 3) { f.ADD_ALL.checked = true; }
                        if (IDnumber == 4) { f.UPLOAD_ALL.checked = true; }
                    }
                    else
                    {
                        if (IDnumber == 1) { f.BROWSE_ALL.checked  = false; }
                        if (IDnumber == 2) { f.INTERACT_ALL.checked = false; }
                        if (IDnumber == 3) { f.ADD_ALL.checked = false; }
                        if (IDnumber == 4) { f.UPLOAD_ALL.checked = false; }
                    }
                }

                function checkcol(IDnumber,status)
                {
                    var f = document.theAdminForm;

                    str_part = '';

                    if (IDnumber == 1) { str_part = 'BROW' }
                    if (IDnumber == 2) { str_part = 'INTE' }
                    if (IDnumber == 3) { str_part = 'ADD_' }
                    if (IDnumber == 4) { str_part = 'UPLO' }

                    for (var i = 0 ; i < f.elements.length; i++)
                    {
                        var e = f.elements[i];

                        if ( (e.name != 'UPLOAD_ALL') && (e.name != 'BROWSE_ALL') && (e.name != 'INTERACT_ALL') && (e.name != 'ADD_ALL') && (e.type == 'checkbox') )
                        {
                            s = e.name;
                            a = s.substring(0, 4);

                            if (a == str_part)
                            {
                                if ( status == 1 )
                                {
                                    e.checked = true;
                                    if (IDnumber == 1) { f.BROWSE_ALL.checked  = true; }
                                    if (IDnumber == 2) { f.INTERACT_ALL.checked = true; }
                                    if (IDnumber == 3) { f.ADD_ALL.checked = true; }
                                    if (IDnumber == 4) { f.UPLOAD_ALL.checked = true; }
                                }
                                else
                                {
                                    e.checked = false;
                                    if (IDnumber == 1) { f.BROWSE_ALL.checked  = false; }
                                    if (IDnumber == 2) { f.INTERACT_ALL.checked = false; }
                                    if (IDnumber == 3) { f.ADD_ALL.checked = false; }
                                    if (IDnumber == 4) { f.UPLOAD_ALL.checked = false; }
                                }
                            }
                        }
                    }
                }

                function checkrow(IDnumber,status)
                {
                    var f = document.theAdminForm;

                    str_part = '';

                    if ( status == 1 )
                    {
                        mystat = 'true';
                    }
                    else
                    {
                        mystat = 'false';
                    }

                    eval( 'f.BROWSE_'+IDnumber+'.checked='+mystat );
                    eval( 'f.INTERACT_'+IDnumber+'.checked='+mystat );
                    eval( 'f.ADD_'+IDnumber+'.checked='+mystat );
                    eval( 'f.UPLOAD_'+IDnumber+'.checked='+mystat );

                    obj_checked(1);
                    obj_checked(2);
                    obj_checked(3);
                    obj_checked(4);
                }

                //-->

                </script>
                ";

        $html .= $this->ipsclass->adskin->add_td_basic( "GLOBAL: All current and future permission masks", "left", "pformstrip" );

        //+-------------------------------------------------------------------------

        if ($browse == '*')
        {
            $html_browse = "<input type='checkbox' onClick='check_all(\"BROW\")' name='BROWSE_ALL' value='1' checked>\n";
        }
        else
        {
            $html_browse = "<input type='checkbox' onClick='check_all(\"BROW\")' name='BROWSE_ALL' value='1'>\n";
        }

        //+-------------------------------------------------------------------------

        if ($interact == '*')
        {
            $html_interact = "<input type='checkbox' onClick='check_all(\"INTE\")' name='INTERACT_ALL' value='1' checked>\n";
        }
        else
        {
            $html_interact = "<input type='checkbox' onClick='check_all(\"INTE\")' name='INTERACT_ALL' value='1'>\n";
        }

        //+-------------------------------------------------------------------------

        if ($add == '*')
        {
            $html_add = "<input type='checkbox' onClick='check_all(\"ADD_\")' name='ADD_ALL' value='1' checked>\n";
        }
        else
        {
            $html_add = "<input type='checkbox' onClick='check_all(\"ADD_\")' name='ADD_ALL' value='1'>\n";
        }

        if ($upload == '*')
        {
            $html_upload = "<input type='checkbox' onClick='check_all(\"UPLO\")' name='UPLOAD_ALL' value='1' checked>\n";
        }
        else
        {
            $html_upload = "<input type='checkbox' onClick='check_all(\"UPLO\")' name='UPLOAD_ALL' value='1'>\n";
        }

        //+-------------------------------------------------------------------------

        $html .= $this->ipsclass->adskin->add_td_row( array( "<b>All current and future permission masks</b>",
                                                             "<center id='mgblue'>$html_browse</center>",
                                                             "<center id='mggreen'>$html_interact</center>",
                                                             "<center id='mgred'>$html_add</center>",
                                                             "<center id='memgroup'>$html_upload</center>",
                                                    )      );

        //+-------------------------------------------------------------------------

        $html .= $this->ipsclass->adskin->add_td_basic( "OR: Adjust permissions per mask below", "left", "pformstrip" );

        $this->ipsclass->DB->query("SELECT * FROM ibf_forum_perms ORDER BY perm_name ASC");

        while ( $data = $this->ipsclass->DB->fetch_row() )
        {
            if ($browse == '*')
            {
                $html_browse = "<input type='checkbox' name='BROWSE_{$data['perm_id']}' value='1' checked onclick=\"obj_checked(1)\">";
            }
            else if ( preg_match( "/(^|,)".$data['perm_id']."(,|$)/", $browse ) )
            {
                $html_browse = "<input type='checkbox' name='BROWSE_{$data['perm_id']}' value='1' checked onclick=\"obj_checked(1)\">";
            }
            else
            {
                $html_browse = "<input type='checkbox' name='BROWSE_{$data['perm_id']}' value='1' onclick=\"obj_checked(1)\">";
            }

            //+----------------------------------------------------------------------------------------

            if ($interact == '*')
            {
                $html_interact = "<input type='checkbox' name='INTERACT_{$data['perm_id']}' value='1' checked onclick=\"obj_checked(2)\">";
            }
            else if ( preg_match( "/(?:^|,)".$data['perm_id']."(?:,|$)/", $interact ) )
            {
                $html_interact = "<input type='checkbox' name='INTERACT_{$data['perm_id']}' value='1' onclick=\"obj_checked(2)\" checked>";
            }
            else
            {
                $html_interact = "<input type='checkbox' name='INTERACT_{$data['perm_id']}' value='1' onclick=\"obj_checked(2)\">";
            }

            //+----------------------------------------------------------------------------------------

            if ($add == '*')
            {
                $html_add = "<input type='checkbox' name='ADD_{$data['perm_id']}' value='1' checked onclick=\"obj_checked(3)\">";
            }
            else if ( preg_match( "/(?:^|,)".$data['perm_id']."(?:,|$)/", $add ) )
            {
                $html_add = "<input type='checkbox' name='ADD_{$data['perm_id']}' value='1' checked onclick=\"obj_checked(3)\">";
            }
            else
            {
                $html_add = "<input type='checkbox' name='ADD_{$data['perm_id']}' value='1' onclick=\"obj_checked(3)\">";
            }

            //+----------------------------------------------------------------------------------------

            if ($upload == '*')
            {
                $html_upload = "<input type='checkbox' name='UPLOAD_{$data['perm_id']}' value='1' checked onclick=\"obj_checked(4)\">";
            }
            else if ( preg_match( "/(?:^|,)".$data['perm_id']."(?:,|$)/", $upload ) )
            {
                $html_upload = "<input type='checkbox' name='UPLOAD_{$data['perm_id']}' value='1' checked onclick=\"obj_checked(4)\">";
            }
            else
            {
                $html_upload = "<input type='checkbox' name='UPLOAD_{$data['perm_id']}' value='1' onclick=\"obj_checked(4)\">";
            }

            $html .= $this->ipsclass->adskin->add_td_row( array( "<div align='right' style='font-weight:bold'>{$data['perm_name']} &nbsp; <input type='button' id='button' value='+' onclick='checkrow({$data['perm_id']},1)' />&nbsp;<input type='button' id='button' value='-' onclick='checkrow({$data['perm_id']},0)' /></div>",
                                                                 "<center id='mgblue'>$html_browse</center>",
                                                                 "<center id='mggreen'>$html_interact</center>",
                                                                 "<center id='mgred'>$html_add</center>",
                                                                 "<center id='memgroup'>$html_upload</center>",
                                                        )      );
        }

        $html .= $this->ipsclass->adskin->add_td_row( array( "&nbsp;",
                                                             "<center>Select: <input type='button' id='button' value='+' onclick='checkcol(1,1)' />&nbsp;<input type='button' id='button' value='-' onclick='checkcol(1,0)' /></center>",
                                                             "<center>Select: <input type='button' id='button' value='+' onclick='checkcol(2,1)' />&nbsp;<input type='button' id='button' value='-' onclick='checkcol(2,0)' /></center>",
                                                             "<center>Select: <input type='button' id='button' value='+' onclick='checkcol(3,1)' />&nbsp;<input type='button' id='button' value='-' onclick='checkcol(3,0)' /></center>",
                                                             "<center>Select: <input type='button' id='button' value='+' onclick='checkcol(4,1)' />&nbsp;<input type='button' id='button' value='-' onclick='checkcol(4,0)' /></center>",
                                                    )      );

        return $html;
    }


    /**
     * Compile the READ/REPLY/START permission strings for the database
     *
     * @return array Array with keys 'BROWSE', 'INTERACT', 'ADD', 'UPLOAD'
     */
    function compile_registry_perms()
    {
        $r_array = array( 'BROWSE' => '', 'INTERACT' => '', 'ADD' => '', 'UPLOAD' => '' );

        if ($this->ipsclass->input['BROWSE_ALL'] == 1)
        {
            $r_array['BROWSE'] = '*';
        }

        if ($this->ipsclass->input['INTERACT_ALL'] == 1)
        {
            $r_array['INTERACT'] = '*';
        }

        if ($this->ipsclass->input['ADD_ALL'] == 1)
        {
            $r_array['ADD'] = '*';
        }

        if ($this->ipsclass->input['UPLOAD_ALL'] == 1)
        {
            $r_array['UPLOAD'] = '*';
        }

        $this->ipsclass->DB->query("SELECT perm_id, perm_name FROM ibf_forum_perms ORDER BY perm_id");

        while ( $data = $this->ipsclass->DB->fetch_row() )
        {
            if ($r_array['BROWSE'] != '*')
            {
                if ($this->ipsclass->input[ 'BROWSE_'.$data['perm_id'] ] == 1)
                {
                    $r_array['BROWSE'] .= $data['perm_id'].",";
                }
            }
            //+----------------------------
            if ($r_array['INTERACT'] != '*')
            {
                if ($this->ipsclass->input[ 'INTERACT_'.$data['perm_id'] ] == 1)
                {
                    $r_array['INTERACT'] .= $data['perm_id'].",";
                }
            }
            //+----------------------------
            if ($r_array['ADD'] != '*')
            {
                if ($this->ipsclass->input[ 'ADD_'.$data['perm_id'] ] == 1)
                {
                    $r_array['ADD'] .= $data['perm_id'].",";
                }
            }
            //+----------------------------
            if ($r_array['UPLOAD'] != '*')
            {
                if ($this->ipsclass->input[ 'UPLOAD_'.$data['perm_id'] ] == 1)
                {
                    $r_array['UPLOAD'] .= $data['perm_id'].",";
                }
            }
        }

        $r_array['BROWSE']   = preg_replace( "/,$/", "", $r_array['BROWSE']   );
        $r_array['INTERACT'] = preg_replace( "/,$/", "", $r_array['INTERACT'] );
        $r_array['ADD']      = preg_replace( "/,$/", "", $r_array['ADD']      );
        $r_array['UPLOAD']   = preg_replace( "/,$/", "", $r_array['UPLOAD']   );

        return $r_array;
    }


    /**
     * Save the Fusion Registry permissions
     */
    function save_config()
    {
        // Create the permission strings
        $perms = $this->compile_registry_perms();

        // Create the db update string
        $perms_array = array( 'browse_perms'   => $perms['BROWSE'],
                              'interact_perms' => $perms['INTERACT'],
                              'add_perms'      => $perms['ADD'],
                              'upload_perms'   => $perms['UPLOAD'],
                            );

        $this->ad_registry_loader->update_config( $perms_array );

        $text = "Updated Fusion Registry Permissions";
        $url = 'act='.REGISTRY_URL.'&section=components&menu=permissions';
        $time = 2;

        // Big brother is watching :)
        $this->ipsclass->admin->save_log($text);

        // End of the show, c'ya
        $this->ipsclass->admin->redirect( $url, $text, 0, $time );
    }


    /**
     * Display HTML for Fusion Registry permission management
     *
     * @uses ad_registry_perms::build_registry_perms() Build the HTML for the permission management
     */
    function main_menu()
    {
        $this->ipsclass->html .= $this->ipsclass->skin_acp_global->information_box( "Edit permissions for Fusion Registry", "<b>Fusion Registry access permissions</b><br />(Check box for access, uncheck to not allow access). 'Interact' dictates whether the mask will be able to vote, search, and leave comments in the Registry. 'Add' dictates whether the mask will be able to add new items and modifications to the Fusion Registry. 'Upload' dictates whether the mask will be able to upload images in the Fusion Registry.<p /><b>NOTE:</b> Regardless of permission settings for Guests below they will not be allowed to add a new item to the Fusion Registry.".'<br />&nbsp;' ) . "<br >";

        $code = "config";

        $this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code', $code         ),
                                                                             2 => array( 'act' , REGISTRY_URL  ),
                                                                             3 => array( 'menu', 'permissions' ),
                                                                             5 => array( 'section', 'components' ),
                                                                    )      );

        // Build the Permissions HTML
        $this->ipsclass->adskin->td_header[] = array( "Name"     , "40%" );
        $this->ipsclass->adskin->td_header[] = array( "Browse"   , "15%" );
        $this->ipsclass->adskin->td_header[] = array( "Interact" , "15%" );
        $this->ipsclass->adskin->td_header[] = array( "Add"      , "15%" );
        $this->ipsclass->adskin->td_header[] = array( "Upload"   , "15%" );

        $this->ipsclass->html .= $this->ipsclass->adskin->start_table( "Permission Access Levels" );

        $this->ipsclass->html .= $this->build_registry_perms( $this->registry_config['browse_perms'], $this->registry_config['add_perms'], $this->registry_config['interact_perms'], $this->registry_config['upload_perms'] );

        $this->ipsclass->html .= $this->ipsclass->adskin->end_form("Save");

        $this->ipsclass->html .= $this->ipsclass->adskin->end_table();


        // That's all folk!
        $this->ipsclass->admin->output();
    }
}
?>